package com.shenfa.dao.systemManager.person;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.shenfa.bean.Person;

public class PersonDao {

	@Autowired
	private SqlMapClient sqlMapClient;
	/**
	 * 根据登陆名获取用户对象
	 * @param loginName
	 * @param loginPsw 
	 * @return
	 */
	public Person getByLoginNamePsw(Map<String, String> map) {
		try {
			return (Person) sqlMapClient.queryForObject(
					"person_manager.get_person_namepwd", 
					map);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据人员Id查询登录名
	 * @param personno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getLoginNameBypersonno(String personno) {
		List<String> list;
		try {
			list = sqlMapClient.queryForList("person_manager.getLoginname", personno);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	/**
	 * 添加用户
	 * @param person
	 */
	public void add(Person person) {
		try {
			sqlMapClient.insert("person_manager.add",person);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 添加用户角色部门关系
	 * @param emprole
	 */
	public void addEmprole(Person person) {
		try {
			sqlMapClient.insert("person_manager.addEmprole",person);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 更新用户角色部门关系
	 * 
	 * @author longchensan create by 2011-10-20
	 * 
	 * @param emprole
	 */
	public void updataEmprole(Person person) {
		try {
			sqlMapClient.update("person_manager.updataEmprole",person);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 修改用户信息
	 * @param person
	 */
	public int update(Person person) {
		try {
			return sqlMapClient.update("person_manager.update", person);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 删除用户信息
	 * @param person
	 */
	public int del(String person) {
		try {
			return sqlMapClient.delete("person_manager.del", person);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 获取所有用户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Person> getAll() {
		try{
			return sqlMapClient.queryForList("person_manager.getAll");
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 根据部门查询所属用户
	 * @param person
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Person> listByDepart(Person person,int start,int limit){
		try{
			return sqlMapClient.queryForList("person_manager.listByDepart",person,start,limit);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public int queryCountByDepart(Person person){
		try {
			return (Integer)sqlMapClient.queryForObject("person_manager.queryCount", person);
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 根据用户名查询对应 person
	 * */
	public Person personBynm(String username){
		try{
			return (Person)sqlMapClient.queryForObject("person_manager.get_person_name",username);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public Person personInfos(String personNo){
		try {
			return (Person)sqlMapClient.queryForObject("person_manager.get_personInfos", personNo);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public int checkPersonName(String name) {
		List<Object> list = null; 
		try {
			list = sqlMapClient.queryForList("person_manager.checkName", name);
			if(list.size() == 0)
				return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return 1;
		}
		return 1;
	}
}
